# -*- coding: utf-8 -*-
"""
@Time ： 2024/2/6 13:53
@Auth ： gc
"""
import baostock as bs
import pandas as pd

def get_hs300_stocks():
    """获取沪深300成分股"""
    # 登陆系统
    bs.login()
    rs = bs.query_hs300_stocks()
    hs300_stocks = []

    while (rs.error_code == '0') & rs.next():

        # 获取一条记录，将记录合并在一起

        hs300_stocks.append(rs.get_row_data())
    def remove_em(a):
        return a["code"].split(".")[1]
    result = pd.DataFrame(hs300_stocks, columns=rs.fields)
    result["code"] = result.apply(remove_em, axis=1)
    result.to_csv("stock/data/hs300_stocks.csv")